package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.CourseDao;
import com.example.domain.Course;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.CourseService;
import com.example.tool.DateTool;

/**
 * @author 袁小弘
 * @describe 课程服务实现层
 * @date 2019-12-31
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseDao courseDao;

    /**
     * @describe 添加课程信息
     */
    @Override
    public Result addCourse(Course course) {
        if (course != null) {
            //获取系统当前时间作为记录创建时间
            course.setRecordTime(DateTool.getTimestamp());
        }

        Course course1 = courseDao.save(course);//调用JPA保存实体方法

        if (course1 != null) {
            return ResultFactory.buildSuccessResult(course1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新课程信息
     */
    @Override
    public Result updateCourse(Course course) {

        if (course != null) {
            //获取系统当前时间作为记录更新时间
            course.setUpdateTime(DateTool.getTimestamp());
        }

        Course course1 = courseDao.saveAndFlush(course);//调用JPA保存并刷新实体方法

        if (course1 != null) {
            return ResultFactory.buildSuccessResult(course1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据课程编号删除课程信息
     */
    @Override
    public Result deleteCourseById(String course_Id) {

        int row = courseDao.deleteByCourseId(Integer.parseInt(course_Id));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 查询所有课程信息
     */
    @Override
    public Result queryAllCourses() {
        List<Course> list = courseDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据课程编号查询该课程信息
     */
    @Override
    public Result queryCoursesById(String course_Id) {
        List<Course> course = courseDao.findByCourseId(Integer.parseInt(course_Id));
        if (course != null) {
            return ResultFactory.buildSuccessResult(course);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }

    }

    /**
     * @describe 通过Excel批量添加课程信息
     */
    @Override
    public Result addCourseByExcel(List<Course> list) {
        List<Course> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = courseDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 课程信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;
        if (info[0].equals("courseName")) {

            list = courseDao.findByCourseNameLike("%" + info[1] + "%");

        } else if (info[0].equals("courseCredit")) {

            list = courseDao.findByCourseCreditLike("%" + info[1] + "%");

        } else if (info[0].equals("courseHour")) {

            list = courseDao.findByCourseHourLike("%" + info[1] + "%");

        } else if (info[0].equals("typeName")) {

            list = courseDao.findByTypeNameLike("%" + info[1] + "%");

        }
        System.out.println(info.toString() + "like==========" + list);

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据课程编号批量删除课程信息
     */
    @Override
    public Result deleteCourseByIds(Map<String, String> courseIds) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (courseIds != null) {
            String ids = courseIds.get("courseIds");
            String[] id = ids.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = courseDao.deleteByCourseIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有课程信息和课程类型信息
     */
    @Override
    public Result queryAllCourseAndType() {

        List<Map<String, Object>> list = courseDao.queryAllClassAndTerm();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }


}
